package org.fjsei.yewu.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import java.io.IOException;

@SpringBootApplication
@ComponentScan(basePackages = "org.fjsei.yewu")
public class SqlGeneratorRunner implements CommandLineRunner {

    private final SqlGenerator sqlGenerator;

    public SqlGeneratorRunner(SqlGenerator sqlGenerator) {
        this.sqlGenerator = sqlGenerator;
    }

    public static void main(String[] args) {
        SpringApplication.run(SqlGeneratorRunner.class, args);
    }

    @Override
    public void run(String... args) throws IOException {
        String outputDir = args.length > 0 ? args[0] : "./sql-scripts";
        
        System.out.println("=== SQL生成工具 ===");
        System.out.println("输出目录: " + outputDir);
        System.out.println("开始扫描实体类...");
        
        try {
            sqlGenerator.generateSqlScripts(outputDir);
            System.out.println("✅ SQL脚本生成成功！");
            System.out.println("\n生成的文件:");
            System.out.println("📄 " + outputDir + "/01-create-tables.sql - 建表脚本");
            System.out.println("📄 " + outputDir + "/99-drop-tables.sql - 删表脚本");
            System.out.println("📄 " + outputDir + "/00-README.md - 说明文档");
            System.out.println("\n⚠️  使用提醒:");
            System.out.println("1. 请在生产环境使用前仔细检查SQL语句");
            System.out.println("2. 建议先在测试环境验证脚本");
            System.out.println("3. 删表脚本请谨慎使用");
        } catch (Exception e) {
            System.err.println("❌ SQL生成失败: " + e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
    }
}